﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.Move
{
    public partial class FrmMateHouseMove : Form
    {
        private string DHouseCode = "";
        private string MoveCode = "";
        DataRow[] dr = null;
        public FrmMateHouseMove(DataRow[] _dr)
        {
            InitializeComponent();
            dr = _dr;
            lab_title.Text = dr[0]["MoveName"].ToString();
            this.Text = lab_title.Text;
            txt_disHouse.Text = dr[0]["dhousename"].ToString();
            DHouseCode = dr[0]["dhousecode"].ToString();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        BarcodeUtils obj = new BarcodeUtils();

        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                BarcodeUtils.BarObject barobj = obj.AnalysisBarcode(Utils.AnalysisSerPath, txt_reelid.Text);

                //检查当前reel是否存在
                DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, "select *,b.housename from MdcDatVStorage a inner join sysdathouse b on a.housecode=b.housecode where ReelID='" + barobj.reelId + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
                if (dt.Rows.Count == 0)
                {
                    lab_msg.Text = "料卷不存在.";
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    return;
                }
                //判断料卷是否可用

                //是否是MSD物料

                string ReelHouseCode = dt.Rows[0]["housecode"].ToString();
                bool flag = false;
                foreach (var item in dr)
                {
                    string[] array = item["shousecode"].ToString().Split(',');
                    foreach (string items in array)
                    {
                        if (items == ReelHouseCode)
                        {
                            flag = true;
                            break;
                        }
                    }

                }
                if (!flag)
                {
                    lab_msg.Text = "料卷所在仓位【" + dt.Rows[0]["housename"].ToString() + "】不在设定的源仓位中.";
                    lab_msg.ForeColor = Color.Red;
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    return;
                }

                AppropriationSheet appSheet = new AppropriationSheet() { MType = "0", PartNumber = dt.Rows[0]["partnumber"].ToString(), WOCode = dt.Rows[0]["wocode"].ToString(), POCode = dt.Rows[0]["pocode"].ToString(), ReelID = barobj.reelId, Qty = dt.Rows[0]["qty"].ToString(), HouseCode = DHouseCode, FromHouse = ReelHouseCode, Description = lab_title.Text, User = Utils.uContext.UserNamek__BackingField };
                
                string sql = string.Format(@"insert into [MdcDatInOutLog](WOCode,sfcno,PartNumber,HouseCode,FromHouse,Reelid,MType,Qty,line,laneno,machineid,slot,side,[DESCRIDE],Creator,Createtime,pocode)
values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}',getdate(),'{15}');
", appSheet.WOCode, appSheet.SfcNo, appSheet.PartNumber, appSheet.HouseCode, appSheet.FromHouse,
appSheet.ReelID, appSheet.MType, appSheet.Qty, appSheet.Line, appSheet.LaneNo, appSheet.MachineID, appSheet.Slot, appSheet.Side, appSheet.Description, appSheet.User, appSheet.POCode);
                //增加SyssystemLog日志 nancy 2017.07.26
                sql+=" insert SyssystemLog(Fguid,line,WoCode,SfcNo,ProuctCode,PartNumber,ReelID,pocode,SupplierID,HouseCode,Qty,Remarks,Creator) ";
                sql += "select newid(),line,wocode,sfcno,'',PartNumber,ReelID,pocode,SupplierID,'" + DHouseCode + "',Qty,'PDA物料仓库调拨转移,原始仓库'+HouseCode, '{14}' from mdcdatvstorage where reelid='{5}'";
                sql+= " update mdcdatvstorage set housecode='" + DHouseCode + "',sfcno='',wocode='' where reelid='" + barobj.reelId + "' ";
                //开始进行调拨转移
                dt = Utils.NMS.QueryDataTable(Utils.uContext, sql, new CIT.WMS.MESWcf.CmdParameter[0]);

                lab_msg.Text = txt_reelid.Text + " 操作成功";
                txt_reelid.SelectAll();
                txt_reelid.Focus();
            }
        }
    }
}